<?php

/**
 * Created by PhpStorm.
 * User: Lin
 * Date: 2017/3/17 0017
 * Time: 下午 15:24
 */
Class OrderAction extends CommonAction
{

    //显示提交订单首页视图
    public function addOrder()
    {
        if (!IS_POST) {
            halt('页面不存在');
        }
        foreach ($_POST as $k => $v) {
            $goodsArr[] = array(
                'goods_id' => $k,
                'num' => $v
            );
        }
        $sum = 0;
        foreach ($goodsArr as $k => $v) {
            $goodsArr[$k]['goods'] = M('goods')->where(array('id' => $v['goods_id']))->find();
            $sum += $v['num'] * $goodsArr[$k]['goods']['shop_price'];
        }
        $data['goodsArr'] = $goodsArr;
        $data['sum'] = $sum;

        $where = array(
            'is_val' => 1
        );
        $data['payment'] = M('payment')->where($where)->select();
        $data['shipping'] = M('shipping')->where($where)->select();
        $data['store'] = M('store')->where($where)->select();

        $this->data = $data;
        $this->display();
    }
    
    //显示订单列表视图
    public function showList(){
        import('Class.Order', APP_PATH);
        import('Class.Payment', APP_PATH);
        $where=array(
            'member_id'=>$_SESSION['userId']
        );
        $orderArr=Order::getOrderArr($where);
        foreach($orderArr as $k=>$v){
            $orderArr[$k]['payment']=Payment::getPaymentName($v['payment_id']);
            $orderArr[$k]['pay_status']=Payment::getPaymentStatusName($v['pay_status']);
            $orderArr[$k]['order_status']=Order::getOrderStatusName($v['order_status']);
        }
        $this->orderArr=$orderArr;
        $this->display();
    }
    
    //显示订单详细信息视图
    public function orderInfo(){
        import('Class.Order', APP_PATH);
        import('Class.Payment', APP_PATH);
        $orderId=$_GET['orderId'];
        $orderInfo=Order::getOrderInfo($orderId,$_SESSION['userId']);
        foreach ($orderInfo['order_goods'] as $k=>$v) {
            $orderInfo['order_goods'][$k]['goods']=M('goods')->where(array('id' => $v['goods_id']))->find();
        }
//
        $orderInfo['payment']=M('payment')->where('id='.$orderInfo['payment_id'])->getField('name');
        $orderInfo['shipping']=M('shipping')->where('id='.$orderInfo['shipping_id'])->getField('name');
        $orderInfo['store']=M('store')->where('id='.$orderInfo['store_id'])->getField('address');
        $orderInfo['order_status']=Order::getOrderStatusName($orderInfo['order_status']);
        $orderInfo['pay_status']=Payment::getPaymentStatusName($orderInfo['pay_status']);
        $this->data=$orderInfo;
        $this->display();
    }
    
    //运行取消订单
    public function cancel(){
        $orderId=$_POST['orderId'];
        $userId=$_SESSION['userId'];
        import('Class.Order',APP_PATH);
        $result=Order::changeOrderStatus(ORDER_STATUS_3,$orderId,$userId);
        if($result){
            $this->success('取消成功',U(GROUP_NAME.'/Order/showList'));
        }else{
            $this->error('取消失败',U(GROUP_NAME.'/Order/showList'));
        }
    }

    //运行订单提交
    public function checkout()
    {
        if (!IS_POST) {
            halt('页面不存在');
        }
        import('Class.Order', APP_PATH);
        $data = Order::OrganizationOrderData($_POST);
        $id = Order::saveOrder($data);
//        var_dump($id);exit();

        if ($id) {
            foreach ($data['order_goods'] as $k => $v) {
                $data['order_goods'][$k]['order_id'] = $id;
            }
            if (!M('order_goods')->addAll($data['order_goods'])) {
                $this->error('提交失败，请稍后重试');
            } else {
                $db = M('cart');
                $Db=M('goods');
                foreach ($data['order_goods'] as $v) {
                    $where = array('user_id' => $_SESSION['userId'], 'goods_id' => $v['goods_id']);
                    $goods = $db->where($where)->find();
                    if ($goods) {//存在该商品 从购物车中删除
                        $db->where($where)->delete();
                    }
                    $Db->where(array('id'=>$v['goods_id']))->setInc('sale_num',$v['num']);
                }
                if ($data['payment_id'] == 3 && $data['shipping_id'] != 4) {
                    header("Content-Type: text/html; charset=UTF-8");
                    echo '请等待工作人员的确认&nbsp;&nbsp;&nbsp;'.'<a href="'.U(GROUP_NAME.'/Order/showList').'">返回订单列表</a>';
                } else {
                    $data=array(
                        'id'=>$id,
                        'pay_status'=>PAY_STATUS_1,
                        'pay_time'=>time(),
                    );
                    M('order')->save($data);
                    $this->success('此处省略支付', U(GROUP_NAME . '/Order/showList'));
                }
            }
        } else {
            $this->error('提交失败，请稍后重试');
        }
    }

    //运行需求登记
    public function addRX(){
        import('ORG.Net.UploadFile');
        $upload = new UploadFile(array('autoSub'=>true,'subType'=>'date'));// 实例化上传类
        $upload->maxSize  = 3145728 ;// 设置附件上传大小
        $upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->savePath =  '../rx_images/';// 设置附件上传目录
        if(!$upload->upload()) {// 上传错误提示错误信息
            $this->error($upload->getErrorMsg());
        }else{// 上传成功 获取上传文件信息
            $info = $upload->getUploadFileInfo();
        }
        $data=array(
            'add_time'=>time(),
            'order_status'=>ORDER_STATUS_0,
            'rx_status'=>RX_STATUS_0,
            'rx_sn'=>'rx'.get_order_sn(),
            'member_id'=>session('userId'),
            'rx_image'=>$info[0]['savepath'].$info[0]['savename']
        );
        $RX=array_merge($_POST,$data);
        if(M('rx')->add($RX)){
            $this->success('登记成功，请等待工作人员联系',U(GROUP_NAME.'/Order/rxList'),5);
        }
    }

    //显示需求登记列表视图
    public function rxList(){
        $userId=session('userId');
        $rxArr=D('RxRelation')->relation('store')->order('add_time DESC')->where('member_id='.$userId)->select();
        foreach ($rxArr as $k=>$v){
            $rxArr[$k]['order_status']=constant('ORDER_STATUS_'.$v['order_status'].'_NAME');
            $rxArr[$k]['rx_status']=constant('RX_STATUS_'.$v['rx_status'].'_NAME');
        }
//        var_dump($rxArr);exit();
        $this->rxArr=$rxArr;
        $this->display();
    }
}